Published Feb 18, 2020 by Alex C-G
I’m a terminal junkie, and a heavy user of Neovim. This leads to a couple of issues when I’m using sudo:
nvim
aliased to vim
in my aliases file (which I source from both .bashrc and .zshrc, so I have consistency). Sudo doesn’t respect aliases by default, so opens up vanilla Vim
sudo nvim
it brings up Neovim, but just uses the boring default settings (since I don’t have anything set up for the root user)
What I want: I type sudo vim
and it opens up Neovim with my user settings
I’ve finally worked out a fix. In your .zshrc (or .bashrc or .whateverrc) file:
alias sudo='nocorrect sudo -E '
Now, when I type sudo vim foo.txt
, it brings up Neovim with my own settings!
nocorrect
- required by zsh for autocorrect to work with sudo
-E
- tells sudo to respect the environment it’s being started in (i.e. the environment with my dotfiles)